#include "RoketObject.h"

int RoketObject::names[] = {0,0,0};
bool RoketObject::compiled = false;


RoketObject::RoketObject(){}
RoketObject::~RoketObject(){}


void RoketObject::LoadLists()
{

names[0] = glGenLists(3); 
names[1] = names[0] + 1;
names[2] = names[1] + 1;

//disegno il puntatore
glNewList(names[0],GL_COMPILE);
     gluCylinder(state->quad_obj,0.22,0.22,2,20,20);
     glRotatef(180,1,0,0);
     glutSolidCone(0.22,1.5,5,5);
     glTranslatef(0,0,-2);
     glutSolidCone(0.27,2.5,20,20);
     glRotatef(-180,1,0,0);
     glTranslatef(0,0,2);
glEndList();


//disegno tappeto di fumo
glNewList(names[1],GL_COMPILE);
   glBegin(GL_QUADS);
              glTexCoord2f(0.0f, 1.0f);   glVertex3f(-.7,-.7,0);
              glTexCoord2f(0.0f, 0.0f);   glVertex3f(-.7,1.5,0);
              glTexCoord2f(1.0f, 0.0f);   glVertex3f(1.5,1.5,0 );
              glTexCoord2f(1.0f, 1.0f);   glVertex3f(1.5,-.7,0 );
   glEnd();
glEndList();  

//disegno la sfera di fuoco e abilito il blending
glNewList(names[2],GL_COMPILE);
        glColor4f(1, 0, 1.0, 1);     
        glBlendFunc(GL_SRC_ALPHA,GL_ONE);       
        glEnable(GL_BLEND);         
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_FUOCO]);
        gluSphere(state->quad_obj,0.3,10,10);
glEndList();

compiled = true;  
}
void RoketObject::DoIdle()
{
    
  if (shooted)
  {
   
    if (pos.y < state->world->GetHeight(pos.x,pos.z))
     {
        state->targ->CheckCollision(type,pos.x,-pos.y,pos.z);
        pos.x -= dx*vel;
        pos.z -= dz*vel;
        pos.y -= dy;
     
        
        dy -= ROKET_SHOT_DEC;       //spiazzamento verticale
        vel = vel * ROKET_SHOT_VEL;  //velocità del missile
        
        angZ_cer = rand() % 360;
        angZ_cer1 = rand() % 360;
     }
    else
     {
        //genero le esplosioni
        Generate_Explosion(-pos.x , -pos.y,-pos.z);
        int segno = rand()%2;
        if (segno == 0) segno = -1; 
        int exps;
         
        
        if (type == HEAVY_ROKET)  exps = rand()%7;      
        else                      exps = rand()%2;     
        
        
        
         
        for (int i = 0; i < exps; i++){
             float dx = rand()% 65 * segno;
             float dz = rand()% 65 * segno ; 
             Generate_Explosion(-pos.x - dx , -pos.y,-pos.z - dz);    
        }
        
        if(!fut_visible) visible = false;
        else
          {
           SetDisp(or_disp[0],or_disp[1],or_disp[2]); //rimetto il missile in posizione iniziale
           Init();
          }
       
       
     }
  }
}


void RoketObject::Init()
{
     
    //se il missile è sparato
    shooted = false;
    fut_visible = true;
    visible = true;
    vel = 1;
    if (!compiled) LoadLists();
}

void RoketObject::SetPosDriection(float x,float y,float z,float angY)
{ 
    pos.angY = angY;
    //calcolo la posizione da dove far partirelo sparo
    float dx = or_disp[0] * cos(state->cam_angY* PI_DIV_180);
    float dz = or_disp[0] * sin(state->cam_angY* PI_DIV_180);
    
    float dx2 = cos(pos.angY* PI_DIV_180);
    float dz2 = sin(pos.angY* PI_DIV_180);
    
    if (!shooted)
    {
        pos.x = x + dx; //+ dx2;
        pos.y = y + or_disp[1];
        pos.z = z + dz;// + dz2;
     }
} 
        
        

void RoketObject::Draw(){
if (visible)
{
glPushMatrix();
     glEnable(GL_COLOR_MATERIAL);
     
     if (type == HEAVY_ROKET)      glColor4f(0.3,0.0,0.05,0.5);
     else                          glColor4f(0.3,0.3,0.3,0.5);
    

     //disegno il missile
     //if (!shooted) glRotatef(pos.angY,0,1,0); 
     glTranslatef(-pos.x ,-pos.y ,-pos.z);
     glRotatef(-state->cam_angY,0,1,0);
     glCallList(names[0]);
    
    

   
    if (shooted)
    {
        //disegno il fuoco
        glCallList(names[2]);
        
        //disegno il fumo 
        glTranslatef(0,0.5,1);
        glRotatef(angZ_cer,0,0,1); 
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_CER1]);
        glCallList(names[1]);
        glRotatef(-angZ_cer,0,0,1);
        
        //disegno il fumo 
        glTranslatef(0,0.5,1); 
        glRotatef(angZ_cer1,0,0,1);
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_CER]);
        glScalef(1.1,1.1,1.1);
        glCallList(names[1]);
    
        //disegno il fumo 
        glTranslatef(0,0.5,1); 
        glRotatef(angZ_cer1,0,0,1);
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_CER]);
        glScalef(1.2,1.2,1.2);
        glCallList(names[1]);
           
         
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_BLEND); 
   }
 
glDisable(GL_COLOR_MATERIAL);    
glPopMatrix();
}    
}

void RoketObject::Shoot()
{
  dy = ROKET_SHOT_HEIGHT;   //spostamento verticale
  dx = sin(state->cam_angY * PI_DIV_180);   //direzione pesata
  dz = -cos(state->cam_angY * PI_DIV_180);   //direzione pesata
  Generate_Smoke(-pos.x,-pos.y,-pos.z);
  shooted = true;
}

void RoketObject::Generate_Smoke(float x,float y,float z)
{
   SmokeObject * s = new SmokeObject(state);
   s->Init();
   s->setPos(x,y,z);
   s->setScale(1);
   
    bool flag = false;
    for (int i = 0;(!flag && i <= state->smoke_counter);i++)
          if (state->smokes[i] == NULL) state->smokes[i] = s,flag = true;
   
   state->smoke_counter++;
}

void RoketObject::Generate_Explosion(float x,float y,float z)
{
   ExplosionObject * s = new ExplosionObject(state);
   s->Init();
   s->setPos(x,y,z);
   
   bool flag = false;
   for (int i = 0;(!flag && i <= state->explosions_counter);i++)
          if (state->explosions[i] == NULL) state->explosions[i] = s,flag = true;

   state->explosions_counter++;
}


